package com.atguigu.java;

/**
 * @author yihu
 * @create 2021-03-01 17:32
 * @email: 1057202547@qq.com
 * @dec: 商品类
 */
public class Goods implements Comparable{
    private  String name;
    private  double price;

    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

    //指明商品比较大小的方式  价格相同时名称排序
    @Override
    public int compareTo(Object o) {
       if(o instanceof  Goods){
           Goods goods = (Goods) o;
           //方式二
           //Double.compare(this.price,goods.price)
           if(this.price > goods.price){
               return  1;
           }else  if(this.price < goods.price){
               return  -1;
           }else {
               //return  0;
              return this.name.compareTo(goods.getName());
           }
       }
       throw new RuntimeException("传入的数据类型不一致");
    }

    @Override
    public String toString() {
        return "Goods{" + "name='" + name + '\'' + ", price=" + price + '}';
    }
}
